package com.gavin.plugin.database.help;

import com.gavin.utils.Utility;

public class DatabaseUtils {
	
	/**
	 * 根据环境变量获取真实的url
	 * @param strURI
	 * @return
	 */
	public static String getRealURIByEnv(String strURI) {
		String strNewURI = strURI;
		int nIndex = 0;
		while (true) {
			int nFrom = strNewURI.indexOf('{', nIndex);
			if (nFrom < 0) {
				break;
			}
			int nTo = strNewURI.indexOf('}', nFrom + 1);
			if (nTo < 0) {
				throw new RuntimeException("");
			}

			String strEnvKey = strNewURI.substring(nFrom, nTo + 1);
			String strEnvValue = Utility.getEnvValue(strEnvKey);
			strNewURI = strNewURI.substring(0, nFrom) + strEnvValue + strNewURI.substring(nTo + 1);
			nIndex = nFrom + strEnvValue.length();
		}

		return strNewURI;
	}
	
	
//	public static void main(String[] args) {
//		System.out.println(DatabaseUtils.getRealURIByEnv("jdbc:mysql://writePay.wxh.mall.mysql:3306/{pay}?useUnicode=true&amp;characterEncoding=utf8&amp;useJDBCCompliantTimezoneShift=true&amp;useLegacyDatetimeCode=false&amp;serverTimezone=Hongkong"));
//	}
}
